const app = getApp();

import regeneratorRuntime from '../../../common/vendor/runtime';

Page({
  data: {
    currentQ: 0,
    totalQuizzes: 10,
    score: 80,
    page: "test",
    withAd: !0,


    categoryLists: [],
    questionLists: [],
    answers: [],

  },

  onLoad() {
    this.init();
  },

  onShareAppMessage() {
    const { shareCover: imageUrl = '', shareTitle: title = '' } = this.data;
    return {
      title,
      path:'/cm_garbage_query/pages/search/search',
      imageUrl
    };
  },

  init() {
    this.getCategoryLists();
    this.getQuestions();
    this.getSetting();
  },

  select(e) {
    let { answers = '', currentQ = 0, totalQuizzes = 0 } = this.data;
    const { categoryitem: chooseAnswer = '', quizzesitem: correctAnswer = '' } = e.currentTarget.dataset;
    let score = 0;

    answers[currentQ] = {
      title: correctAnswer.title,
      categoryname: correctAnswer.categoryname,
      chooseName: chooseAnswer.title,
      isRight: chooseAnswer.id === correctAnswer.categoryid ? true : false,
    }

    if (currentQ >= totalQuizzes - 1) {
      score = answers.filter(item => item.isRight);
      this.setData({
        score: score.length * 10
      })
    }

    this.setData({
      answers,
      currentQ: ++currentQ,
    })


  },

  restart() {
    this.setData({
      currentQ: 0,
      answers: [],
      score: 0,
    })
    this.init();
  },

  share() {
  },

  getCategoryLists() {
    app.util.request({
      module: 'cm_garbage_query',
      url: 'entry/wxapp/index',
      data: {
        op: 'getCategoryLists'
      },
      showLoading: true,
      success: res => {
        const categoryLists = res.data.data || '';
        console.log('========categoryLists', categoryLists);

        categoryLists && this.setData({
          categoryLists
        })
      },
    });
  },

  getQuestions() {
    app.util.request({
      module: 'cm_garbage_query',
      url: 'entry/wxapp/index',
      data: {
        op: 'getQuestions'
      },
      showLoading: true,
      success: res => {
        const questionLists = res.data.data || '';
        const totalQuizzes = questionLists.length;
        questionLists && this.setData({
          questionLists,
          totalQuizzes
        })
      },
    });
  },

  getSetting() {
    app.util.request({
      module: 'cm_garbage_query',
      url: 'entry/wxapp/index',
      data: {
        op: 'getSetting'
      },
      showLoading: true,
      success: res => {
        const { share_cover: shareCover = '', share_title: shareTitle ='' } =  res.data.data;
       this.setData({
          shareCover,
          shareTitle
        })
      },
    });
  }
});